# import json
# import logging
# import re
# import uuid as uuid_module
# from django.views import View
# from django.http.request import HttpRequest
# from django.http import JsonResponse
#
# from .models import TicketModel as TicketModel_Class
# from .utils import idmi_nlp_tool
# from .utils import mapping_brand_series
#
#
# TicketModel = TicketModel_Class()
#
# class Settings__:
#     SOURCEID_LIST = [
#         "complaint_12365auto",
#         "complaint_315auto",
#         "complaint_315qc",
#         "complaint_aqsiqauto",
#         "complaint_autohome",
#         "complaint_qctsw",
#         "complaint_qiche365",
#         "complaint_qichemen",
#
#         'cuzu_autohome',
#         'cuzu_dongchedi',
#
#         "forum_autohome",
#         "forum_dongchedi",
#         "forum_pcauto",
#         "forum_xcar",
#         "forum_yiche",
#     ]
#
# def get_keys(key, dic, ret=[]):
#     '''
#     给定一个key, 全面遍历dic, 最终将所有匹配上 key 的value全部拿出来
#     1. 其实就是一个循环遍历，将字典下面每个位置都遍历出来
#     '''
#     if key in dic.keys():
#         ret.append(dic[key])
#     else:
#         for dict_key in dic.keys():
#             if isinstance(dic[dict_key], dict):
#                 get_keys(key, dic[dict_key], ret)
#             elif isinstance(dic[dict_key], list):
#                 for _ in dic[dict_key]:
#                     if isinstance(_, dict):
#                         get_keys(key, _, ret)
#     return ret
#
# def is_contain_chinese(check_str):
#     """
#     判断字符串中是否包含中文
#     :param check_str: {str} 需要检测的字符串
#     :return: {bool} 包含返回True， 不包含返回False
#     """
#     for ch in check_str:
#         if u'\u4e00' <= ch <= u'\u9fff':
#             return True
#     return False
#
#
# class StoreTicket(View):
#     '''
#     store the complaint sent
#     {
#         "sourceId": "complaint_12365auto",
#         "url": "http://m.12365auto.com/zlts/20231031/1041369.shtml",
#         "_url_unique": "/zlts/20231031/1041369.shtml",
#         "source_web_origin": "12365auto",
#         "save_source_image": "yes",
#         "info_category": "complaint",
#         "title": "上汽大众途昂X变速箱过不平路面有咔咔异响",
#         "text": "车辆行驶减速带，时候变速箱出现咔咔异响，去经销商检查告知没有问题，这几天行驶观察还是有咔咔异响声音。",
#         "brand_src": "上汽大众",
#         "series_src": "途昂X",
#         "model_src": "2023款 380TSI 四驱 尊崇豪华版",
#         "image_urls": "[]"
#     }
#     '''
#
#     def post(self, request: HttpRequest) -> JsonResponse:
#         sourceId = request.POST.get('sourceId')
#         if sourceId in [
#             'complaint_12365auto', 'complaint_315auto', 'complaint_315qc', 'complaint_aqsiqauto',
#             'complaint_autohome', 'complaint_qctsw', 'complaint_qiche365', 'complaint_qichemen',
#             'forum_autohome', 'forum_dongchedi', 'forum_pcauto', 'forum_xcar', 'forum_yiche'
#         ]:
#             # 非CuZu类型的投诉数据
#             TicketModel.sourceId = sourceId
#             TicketModel.uuid = str(uuid_module.uuid4()).replace('-', '')
#
#             TicketModel.source_web_origin = request.POST.get('source_web_origin')
#             TicketModel.save_source_image = request.POST.get('save_source_image')
#             TicketModel.info_category = request.POST.get('info_category')
#
#             TicketModel.URL = request.POST.get('url')[:400]
#             TicketModel.url_unique = request.POST.get('_url_unique')[:400]
#
#             # preprocess the content of title and text crawled from target websites
#             __title_zh = request.POST.get('title')[:400]
#             __text_zh = request.POST.get('text')[:1000]
#             TicketModel.title_zh = __title_zh
#             TicketModel.text_zh = __text_zh
#
#             # preprocess the title and text to get the function group und problem
#             # TODO 在这里加上权限，只有当爬取的品牌在需求品牌范围内的才需要翻译，其他的内容不需要翻译
#             temp__brand_src = request.POST.get('brand_src', '')
#             FLAG_translate_needed = False
#             for __ in [
#                 '大众', 'VW', 'vw',
#                 '特斯拉', 'Tesla', 'TESLA',
#                 '小鹏', 'Xpeng',
#                 '埃安', 'AION',
#                 '赛力斯', 'AITO'
#             ]:
#                 if __ in temp__brand_src:
#                     FLAG_translate_needed = True
#                     break
#
#             nlp_res = idmi_nlp_tool.response(
#                 title=__title_zh, text=__text_zh,
#                 if_translate=FLAG_translate_needed,
#                 sourceId=sourceId
#             )
#             TicketModel.FLAG_function_group_problem = nlp_res['FLAG_function_group_problem']
#             TicketModel.FLAG_ncvqs = nlp_res['FLAG_ncvqs']
#
#             TicketModel.title_en = nlp_res['title_en']
#             TicketModel.text_en = nlp_res['text_en']
#             TicketModel.FLAG_translate_to_en = nlp_res['translate_success']
#
#             res__Quality_labels = nlp_res['Quality_labels']
#             res__Ncvqs_labels = nlp_res['Ncvqs_labels']
#
#             if len(res__Quality_labels)>=5:
#                 Quality_labels = res__Quality_labels[:5]
#             else:
#                 for _ in range(5 - len(res__Quality_labels)):
#                     res__Quality_labels.append('@@')
#                 Quality_labels = res__Quality_labels
#             TicketModel.function_group_1 = Quality_labels[0].split('@@')[0]
#             TicketModel.problem_1 = Quality_labels[0].split('@@')[1]
#             TicketModel.function_group_2 = Quality_labels[1].split('@@')[0]
#             TicketModel.problem_2 = Quality_labels[1].split('@@')[1]
#             TicketModel.function_group_3 = Quality_labels[2].split('@@')[0]
#             TicketModel.problem_3 = Quality_labels[2].split('@@')[1]
#             TicketModel.function_group_4 = Quality_labels[3].split('@@')[0]
#             TicketModel.problem_4 = Quality_labels[3].split('@@')[1]
#             TicketModel.function_group_5 = Quality_labels[4].split('@@')[0]
#             TicketModel.problem_5 = Quality_labels[4].split('@@')[1]
#
#             if len(res__Ncvqs_labels)>=2:
#                 Ncvqs_labels = res__Ncvqs_labels[:2]
#             else:
#                 for _ in range(5 - len(res__Ncvqs_labels)):
#                     res__Ncvqs_labels.append('')
#                 Ncvqs_labels = res__Ncvqs_labels
#             TicketModel.ncvqs_1 = Ncvqs_labels[0]
#             TicketModel.ncvqs_2 = Ncvqs_labels[1]
#
#             # preprocess the images
#             __save_images = request.POST.get('save_source_image')
#             if __save_images=='yes':
#                 __images_urls = request.POST.get('image_urls')
#                 __images_urls = json.loads(__images_urls)
#                 if len(__images_urls)>=3:
#                     images_urls = __images_urls[:3]
#                 else:
#                     for _ in range(3 - len(__images_urls)):
#                         __images_urls.append('')
#                     images_urls = __images_urls
#                 TicketModel.image_url_1 = images_urls[0]
#                 TicketModel.image_url_2 = images_urls[1]
#                 TicketModel.image_url_3 = images_urls[2]
#                 TicketModel.already_request_img_content = False
#             else:
#                 TicketModel.image_url_1 = ''
#                 TicketModel.image_url_2 = ''
#                 TicketModel.image_url_3 = ''
#                 TicketModel.already_request_img_content = True
#
#             # preprocess the brand_src and series_src and model_src
#             __brand_src = request.POST.get('brand_src', '')
#             __series_src = request.POST.get('series_src', '')
#             __model_src = request.POST.get('model_src', '')
#             if sourceId in [
#                 'forum_autohome', 'forum_dongchedi', 'forum_pcauto', 'forum_xcar', 'forum_yiche'
#             ]:
#                 TicketModel.brand_src = __brand_src
#                 TicketModel.series_src = __series_src
#                 TicketModel.model_src = __model_src
#                 TicketModel.brand = __brand_src
#                 TicketModel.series = __series_src
#                 TicketModel.model = __model_src
#                 TicketModel.FLAG_brand_model_convert = True
#                 TicketModel.save()
#                 return JsonResponse({'status': 200})
#             else:
#                 TicketModel.brand_src = __brand_src[:40]
#                 TicketModel.series_src = __series_src[:40]
#                 TicketModel.model_src = __model_src[:40]
#                 brand, series = mapping_brand_series.mapping_get(
#                     sourceId,
#                     __brand_src,
#                     __series_src
#                 )
#                 TicketModel.brand = brand
#                 TicketModel.series = series
#                 if len(brand) and len(series):
#                     TicketModel.FLAG_brand_model_convert = True
#                 else:
#                     TicketModel.FLAG_brand_model_convert = False
#
#                 if TicketModel.sourceId.startswith('complaint'):
#                     TicketModel.save()
#                 else:
#                     # 对于论坛类型的数据，只保留有效的数据集，不使用无效数据集
#                     if TicketModel.FLAG_function_group_problem==True or TicketModel.FLAG_ncvqs==True:
#                         TicketModel.save()
#                     else:
#                         ...
#                 return JsonResponse({'status': 200})
#
#         else:
#             if sourceId=='cuzu_autohome':
#                 print("===当前的sourceId = {} ====".format(sourceId))
#                 content_list = json.loads(request.POST.get('content_list'))
#                 if content_list['returncode']==0:
#                     extract_list_1 = get_keys(key='scenecontent', dic=content_list)
#                     extract_list_2 = get_keys(key='feeling', dic=content_list)
#                     scenecontent_list = set()
#                     for _ in extract_list_1:
#                         if is_contain_chinese(_):
#                             scenecontent_list.add(_)
#                     for _ in extract_list_2:
#                         if is_contain_chinese(_):
#                             scenecontent_list.add(_)
#                     scenecontent_list = list(scenecontent_list)
#                     for scenecontent in scenecontent_list:
#                         if len(scenecontent)>10:
#                             CuZuModel.uuid = str(uuid_module.uuid4()).replace('-', '')
#                             CuZuModel.sourceId = sourceId
#                             CuZuModel.source_web_origin = request.POST.get('source_web_origin')
#                             CuZuModel.save_source_image = request.POST.get('save_source_image')
#                             CuZuModel.info_category = request.POST.get('info_category')
#                             CuZuModel.URL = request.POST.get('url')[:400]
#                             CuZuModel.url_unique = request.POST.get('_url_unique')[:400]
#                             CuZuModel.brand_src = request.POST.get('brand_src')
#                             CuZuModel.series_src = request.POST.get('series_src')
#                             CuZuModel.model_src = request.POST.get('model_src')
#                             CuZuModel.brand = request.POST.get('brand_src')
#                             CuZuModel.series = request.POST.get('series_src')
#                             CuZuModel.model = request.POST.get('model_src')
#                             # CuZuModel.concrete_model = True
#                             scenecontent = scenecontent.replace('\r', '').replace('\n', '').replace(' ', '')[:490]
#                             CuZuModel.sentence_zh = scenecontent
#                             scenecontent_en = idmi_nlp_tool.translate_sentence(sentence=scenecontent)['sentence_en']
#                             CuZuModel.sentence_en = scenecontent_en
#                             CuZuModel.FLAG_translate_to_en = True
#                             cuzu_nlp_res = cuzu.cuzu_preprocess(sentence=scenecontent)
#                             CuZuModel.content_nlp_res = json.dumps(
#                                 cuzu_nlp_res, ensure_ascii=False
#                             )
#                             CuZuModel.save()
#                     return JsonResponse({'status': 200})
#                 else:
#                     return JsonResponse({'status': 200})
#             elif sourceId=='cuzu_dongchedi':
#                 """
#                 {"sourceId": "cuzu_dongchedi",
#                 "source_web_origin": "dongchedi",
#                 "save_source_image": "yes",
#                 "info_category": "cuzu",
#                 "_url_unique": "https://m.dongchedi.com/motor/pc/car/series/get_review_list?aid=1230&app_name=automobile_web&series_id=4961&part_id=S0&sort_by=create_time&page=1&count=50@@7269081438723510334@@FAW-VW@@ID.6 CROZZ",
#                 "url": "https://m.dongchedi.com/koubei/7269081438723510334",
#                  "content_list": "【最不满意】\n车机确实拉夸，只能算将就用。好在基本功能都有。自己也没开过什么好车，第一台车是14年大众桑塔纳，第二台大众途安L，这是第三台车了还是大众?。只能说你不能每天都在喊它给你空调、开车窗、这次选择老6，主要是7座是刚需，目前来看这个价位纯电车7座也只有老6可以选择。再加上确实是对大众情有独钟，其他的7座纯电也看过价格高买不起。\n【驾驶感受】\n驾驶感受还算不错。以前一直都是开大众的车型。但没开过后驱的车。特别是在下雨天过高架桥接缝的地方如果是铁板接缝的再加上有点弯基本上都会出现车身稳定系统干预的情况。不知道大家在使用中是不是也这样。\n【续航】\n看了好多车友的帖子，自己也开了一段时间了研究过续航的问题，我正常市区开空调350左右。城市快速路400-450。高速还没跑过。这里只代表个人感受和理解。平原地区的车友续航基本上比在高原地区的车友续航要高一点，这是在同一个人驾驶风格不变的情况下。当然也有车友在相同的城市开得就比别人能耗高，这个其实就和自己的开车风格有关了。
#                  也有把这个车当老头乐来开的续航肯定不错?，但是大家有没有想过你自己在路上跑着的时候限速60前面车就是开得像老头乐一样。心里会不会一万个问候路过。电车用车成本不高。别搞个电车把自己格局弄小了。该怎么用就怎么用。\n【购车经历】\n其实没那么急换车的，主要是看见一汽大众活动去当地4S电了解和深度试驾后，和媳妇商量后。决定用现在用的途安L置换。\n【提车价格】\n经过和4S的斗智斗勇最终价格21.29，贷款15，5年。\n【亮点配置】\nARHUD这个功能确实好用\n电动后备箱也好用，因为以前的车没这功能?\n【最满意】\n这个车算是中规中矩没有什么太大的亮点。但也没得什么短板，要说有就是车机了。\n【乘坐体验】\n以前的车是途安L是紧凑级MPV，车长4.5+。这个车将近4.9，所以纵向空间比途安L大，横向内部空间感觉没途安L大。", "brand_src": "FAW-VW", "series_src": "ID.6 CROZZ", "model_src": "", "image_urls": "[{\"img_type\": 1, \"url\": \"https://p6-dcd-sign.byteimg.com/motor-article-img/2d6b8514c48d41b4aa816de38cbb1ffd~0x2048.image?x-expires=1701480075&x-signature=Yj5bynb%2BxJaWCucckmqaCg%2BrFKU%3D\", \"width\": 3648, \"height\": 2736}, {\"img_type\": 1, \"url\": \"https://p9-dcd-sign.byteimg.com/motor-article-img/8ff4c5d3f6fd4978a92dff47230c3b78~0x2048.image?x-expires=1701480075&x-signature=FtMIw7qvysejdF2cXGPqTCVZ6QE%3D\", \"width\": 3648, \"height\": 2736}, {\"img_type\": 1, \"url\": \"https://p3-dcd.byteimg.com/img/motor-img/ad74654e2e924e65b60d11c3cbe397ae~0x2048.image\", \"width\": 3648, \"height\": 2736}, {\"img_type\": 1, \"url\": \"https://p9-dcd.byteimg.com/img/motor-img/9209fd9ae685438ca235b2f71ebfdf63~0x2048.image\", \"width\": 1920, \"height\": 1080}, {\"img_type\": 1, \"url\": \"https://p3-dcd-sign.byteimg.com/motor-article-img/feba9a5f352741eca3b8a99a67c47951~0x2048.image?x-expires=1701480075&x-signature=UWuC0qbkz3uoc9h%2FGoC08kIHQ04%3D\", \"width\": 3648, \"height\": 2736}, {\"img_type\": 1, \"url\": \"https://p3-dcd.byteimg.com/img/motor-img/b5e3ae7a43c94a0f9b2d627b1b133000~0x2048.image\", \"width\": 3648, \"height\": 2736}, {\"img_type\": 1, \"url\": \"https://p9-dcd-sign.byteimg.com/motor-article-img/2c0d41f7b14747ec99caa644897f03a0~0x2048.image?x-expires=1701480075&x-signature=Vu3xUXzniwK5AQ%2BBweCRxkQRfGc%3D\", \"width\": 3648, \"height\": 2736}]"}
#                 """
#                 print("===当前的sourceId = {} ====".format(sourceId))
#                 content_list = request.POST.get('content_list')
#                 content_list = content_list.encode('utf-8', 'replace').decode('utf-8')
#
#                 content_sentence = content_list.replace('\r', '')\
#                     .replace('\n', '')\
#                     .replace(' ', '')
#                 pattern = r'。|【|】'
#                 result_list = re.split(pattern, content_sentence)
#
#                 for result in result_list:
#                     if len(result)<10:
#                         ...
#                     else:
#                         CuZuModel.sourceId = sourceId
#                         CuZuModel.source_web_origin = request.POST.get('source_web_origin')
#                         CuZuModel.save_source_image = request.POST.get('save_source_image')
#                         CuZuModel.info_category = request.POST.get('info_category')
#
#                         CuZuModel.URL = request.POST.get('url')[:400]
#                         CuZuModel.url_unique = request.POST.get('_url_unique')[:400]
#                         CuZuModel.brand_src = request.POST.get('brand_src')
#                         CuZuModel.series_src = request.POST.get('series_src')
#                         CuZuModel.model_src = request.POST.get('model_src')
#                         CuZuModel.brand = request.POST.get('brand_src')
#                         CuZuModel.series = request.POST.get('series_src')
#                         CuZuModel.model = request.POST.get('model_src')
#                         # CuZuModel.concrete_model = True
#
#                         CuZuModel.uuid = str(uuid_module.uuid4()).replace('-', '')
#                         result = result[:490]
#                         cuzu.cuzu_preprocess(sentence=result)
#                         CuZuModel.sentence_zh = result
#                         CuZuModel.sentence_en = idmi_nlp_tool.translate_sentence(sentence=result)['sentence_en']
#                         CuZuModel.FLAG_translate_to_en = True
#
#                         cuzu_nlp_res = cuzu.cuzu_preprocess(sentence=result)
#                         CuZuModel.content_nlp_res = json.dumps(
#                             cuzu_nlp_res, ensure_ascii=False
#                         )
#                         CuZuModel.save()
#                 return JsonResponse({'status': 200})
#
#             else:
#                 logging.ERROR('ERROR')
#                 return JsonResponse({'status': 200})
#
#
#
#
#
#
#
#
#
#
# '''
#
# class vwNCVQSContentList(View):
#
#     def post(self, request: HttpRequest) -> JsonResponse:
#         ...
#
#
# class SearchTicket_audi(View):
#
#     def post(self, request: HttpRequest) -> JsonResponse:
#         start_date = request.POST.get('start_date')
#         end_date = request.POST.get('end_date')
#         ncvqs_search = request.POST.get('ncvqs_search')
#         model_search = request.POST.get('model_search')
#         language = request.POST.get('language')
#         keyword = request.POST.get('keyword', '')
#
#         if start_date is None or start_date == '':
#             now = timezone.now()
#             start_date = now - timezone.timedelta(hours=24 * 7, minutes=59, seconds=59)
#         else:
#             _start_date_Array = time.strptime(start_date, "%Y%m%d")
#             start_date = time.strftime("%Y-%m-%d %H:%M", _start_date_Array)
#         if end_date is None or end_date == '':
#             end_date = timezone.now()
#         else:
#             _end_date_Array = time.strptime(end_date, "%Y%m%d")
#             end_date = time.strftime("%Y-%m-%d %H:%M", _end_date_Array)
#
#         if ncvqs_search is None:
#             ncvqs_search = ''
#         if model_search is None:
#             model_search = ''
#         if language is None:
#             language = 'zh'
#
#         tar_Model = audi_brand_Model
#         if language == '' or language == 'zh':
#             _res_content_list = tar_Model.objects.filter(
#                 Q(create_time__gte=start_date) &
#                 Q(create_time__lte=end_date) &
#                 Q(
#                     Q(modelsrc_from_platform__icontains=model_search) |
#                     Q(oemsrc_from_platform__icontains=model_search)
#                 ) &
#                 Q(
#                     Q(ncvqs_1__icontains=ncvqs_search) |
#                     Q(ncvqs_2__icontains=ncvqs_search) |
#                     Q(ncvqs_3__icontains=ncvqs_search)
#                 ) & (
#                         Q(title_zh__icontains=keyword) |
#                         Q(text_zh__icontains=keyword)
#                 )
#             ).all().values(
#                 'title_zh', 'text_zh', 'sourceId',
#                 'ncvqs_1',
#                 'ncvqs_2',
#                 'ncvqs_3',
#                 'is_delete',
#                 'modelsrc_from_platform', 'oemsrc_from_platform'
#             )
#             res_content_list = []
#             for r in _res_content_list:
#                 res_content_list.append(r)
#             return JsonResponse({'data': res_content_list,
#                                  'data_length': len(res_content_list),
#                                  'idmi_response_code': IDMI_Response_Code.OK})
#         elif language == 'de':
#             res_content_list = tar_Model.objects.filter(
#                 Q(create_time__gte=start_date) &
#                 Q(create_time__lte=end_date) &
#                 Q(
#                     Q(modelsrc_from_platform__icontains=model_search) |
#                     Q(oemsrc_from_platform__icontains=model_search)
#                 ) &
#                 Q(
#                     Q(ncvqs_1__icontains=ncvqs_search) |
#                     Q(ncvqs_2__icontains=ncvqs_search) |
#                     Q(ncvqs_3__icontains=ncvqs_search)
#                 )
#             ).all().values(
#                 'title_de', 'text_de', 'sourceId',
#                 'ncvqs_1',
#                 'ncvqs_2',
#                 'ncvqs_3',
#                 'is_delete',
#                 'model_convert_en', 'oem_convert_en'
#             )
#             return JsonResponse({'data': res_content_list,
#                                  'data_length': len(res_content_list),
#                                  'idmi_response_code': IDMI_Response_Code.OK})
#         else:
#             res_content_list = tar_Model.objects.filter(
#                 Q(create_time__gte=start_date) &
#                 Q(create_time__lte=end_date) &
#                 Q(
#                     Q(modelsrc_from_platform__icontains=model_search) |
#                     Q(oemsrc_from_platform__icontains=model_search)
#                 ) &
#                 Q(
#                     Q(ncvqs_1__icontains=ncvqs_search) |
#                     Q(ncvqs_2__icontains=ncvqs_search) |
#                     Q(ncvqs_3__icontains=ncvqs_search)
#                 )
#             ).all().values(
#                 'title_en', 'text_en', 'sourceId',
#                 'ncvqs_1',
#                 'ncvqs_2',
#                 'ncvqs_3',
#                 'is_delete',
#                 'model_convert_en', 'oem_convert_en'
#             )
#             return JsonResponse({'data': res_content_list,
#                                  'data_length': len(res_content_list),
#                                  'idmi_response_code': IDMI_Response_Code.OK})
#
# '''
#
#
#
#
#
#
#
#
